package io.kiki.sba.registry.server.clientInterface.remoting.console.handler;


import io.kiki.sba.registry.api.Channel;
import io.kiki.sba.registry.common.model.ClientToClientInterfaceServerNodeId;
import io.kiki.sba.registry.common.model.Response;
import io.kiki.sba.registry.common.model.sessionserver.ClientOffRequest;
import io.kiki.sba.registry.server.clientInterface.connections.ConnectionsService;
import io.kiki.sba.registry.server.clientInterface.registry.ClientInterfaceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;

public final class ClientOffRequestHandler extends AbstractConsoleHandler<ClientOffRequest> {
    private static final Logger logger = LoggerFactory.getLogger(ClientOffRequestHandler.class);

    @Autowired
    protected ConnectionsService connectionsService;
    @Autowired
    protected ClientInterfaceRegistry clientInterfaceRegistry;

    @Override
    public Object doHandle(Channel channel, ClientOffRequest request) {
        List<ClientToClientInterfaceServerNodeId> conIds = connectionsService.getIpConnects(new HashSet<>(request.getIps()));
        clientInterfaceRegistry.clientOff(conIds);
        logger.info("clientOff conIds: {}", conIds);
        return Response.buildSuccessResponse();
    }

    @Override
    public Class interest() {
        return ClientOffRequest.class;
    }

    @Override
    public Object buildFailedResponse(String msg) {
        return Response.buildFailedResponse(msg);
    }
}
